/*----------------------------------------------------------------------
  - File name      : STC8Ax_NVIC.h
  - Author         : Quark Team
  - Update date    : 2020-12-09                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: Header file of NVIC module.
-----------------------------------------------------------------------*/
#ifndef __STC8Ax_NVIC_H_
#define __STC8Ax_NVIC_H_
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
/*--------------------------------------------------------
| @Description: STC8Ax Options                           |
--------------------------------------------------------*/
#include "STC8Ax_REG.h"   
/*--------------------------------------------------------
| @Description: STC8Ax core                              |
--------------------------------------------------------*/
#include "STC8Ax_CORE.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: Interrupt priority                       |
--------------------------------------------------------*/

typedef enum
{
  NVIC_FIX  =  0x00,  //Fixed   priority
  NVIC_PR0  =  0x00,  //Lowest  priority |
  NVIC_PR1  =  0x01,  //Lower   priority |
  NVIC_PR2  =  0x02,  //Higher  priority |
  NVIC_PR3  =  0x03   //Highest priority V
} NVICPri_Type;

/*--------------------------------------------------------
| @Description: EXTI priority define function            |
--------------------------------------------------------*/

#define EXTI0_NVIC_PRI(PRI) { \
IPH = (IPH & 0xFE) | ((PRI & 0x02) >> 1); \
IP  = (IP  & 0xFE) | (PRI & 0x01); }

#define EXTI1_NVIC_PRI(PRI) { \
IPH = (IPH & 0xFB) | ((PRI & 0x02) << 1); \
IP  = (IP  & 0xFB) | ((PRI & 0x01) << 2); }

/*--------------------------------------------------------
| @Description: TIMER priority define function           |
--------------------------------------------------------*/

#define TIMER0_NVIC_PRI(PRI) { \
IPH = (IPH & 0xFD) |  (PRI & 0x02) ; \
IP  = (IP  & 0xFD) | ((PRI & 0x01) << 1);}

#define TIMER1_NVIC_PRI(PRI) { \
IPH = (IPH & 0xF7) | ((PRI & 0x02) << 2); \
IP  = (IP  & 0xF7) | ((PRI & 0x01) << 3); }

/*--------------------------------------------------------
| @Description: UART priority define function            |
--------------------------------------------------------*/

#define UART1_NVIC_PRI(PRI) { \
IPH = (IPH & 0xEF) | ((PRI & 0x02) << 3); \
IP  = (IP  & 0xEF) | ((PRI & 0x01) << 4); }

#define UART2_NVIC_PRI(PRI) { \
IP2H = (IP2H & 0xFE) | ((PRI & 0x02) >> 1); \
IP2  = (IP2  & 0xFE) | (PRI & 0x01); }

/*--------------------------------------------------------
| @Description: COMP priority define function            |
--------------------------------------------------------*/

#define COMP_NVIC_PRI(PRI) { \
IP2H = (IP2H & 0xEF) | ((PRI & 0x02) << 3); \
IP2  = (IP2  & 0xEF) | ((PRI & 0x01) << 4); }

/*--------------------------------------------------------
| @Description: ADC priority define function             |
--------------------------------------------------------*/

#define ADC_NVIC_PRI(PRI) { \
IPH = (IPH & 0xDF) | ((PRI & 0x02) << 4); \
IP  = (IP  & 0xDF) | ((PRI & 0x01) << 5); }

/*--------------------------------------------------------
| @Description: LVD priority define function             |
--------------------------------------------------------*/

#define LVD_NVIC_PRI(PRI) { \
IPH = (IPH & 0xBF) | ((PRI & 0x02) << 5); \
IP  = (IP  & 0xBF) | ((PRI & 0x01) << 6); }

/*--------------------------------------------------------
| @Description: PCA priority define function             |
--------------------------------------------------------*/

#define PCA_NVIC_PRI(PRI) { \
IPH = (IPH & 0x7F) | ((PRI & 0x02) << 6); \
IP  = (IP  & 0x7F) | ((PRI & 0x01) << 7); }

/*--------------------------------------------------------
| @Description: PWM priority define function             |
--------------------------------------------------------*/

#define PWM_CNT_NVIC_PRI(PRI) { \
IP2H = (IP2H & 0xFB) | ((PRI & 0x02) << 1); \
IP2  = (IP2  & 0xFB) | ((PRI & 0x01) << 2); }

#define PWM_ABD_NVIC_PRI(PRI) { \
IP2H = (IP2H & 0xF7) | ((PRI & 0x02) << 2); \
IP2  = (IP2  & 0xF7) | ((PRI & 0x01) << 3); }

/*--------------------------------------------------------
| @Description: SPI priority define function             |
--------------------------------------------------------*/

#define SPI_NVIC_PRI(PRI) { \
IP2H = (IP2H & 0xFD) |  (PRI & 0x02); \
IP2  = (IP2  & 0xFD) | ((PRI & 0x01) << 1); }

/*--------------------------------------------------------
| @Description: I2C priority define function             |
--------------------------------------------------------*/

#define I2C_NVIC_PRI(PRI) { \
IP2H = (IP2H & 0xBF) | ((PRI & 0x02) << 5); \
IP2  = (IP2  & 0xBF) | ((PRI & 0x01) << 6); }

/*--------------------------------------------------------
| @Description: External interrupt Trigger define        |
--------------------------------------------------------*/

typedef enum
{
  EXTI_Tri_Edge    = 0x00,  //Both rising and falling edges triggered
  EXTI_Tri_Falling = 0x01    //Falling edge trigger
} EXTITri_Type;

/*--------------------------------------------------------
| @Description: COMP interrupt Trigger enum              |
--------------------------------------------------------*/

#define COMP_TRIState_Init(TriState) CMPCR1 = (CMPCR1 & 0xCF)|(TriState)

typedef enum
{
  COMP_Tri_None    = 0x00,
  COMP_Tri_Falling = 0x10,
  COMP_Tri_Rising  = 0x20,
  COMP_Tri_Edge    = 0x30
} COMPTri_Type;

/*--------------------------------------------------------
| @Description: COMP interrupt Trigger enum              |
--------------------------------------------------------*/

typedef enum
{
  PCA_Tri_None    = 0x00,
  PCA_Tri_Falling = 0x11,
  PCA_Tri_Rising  = 0x21,
  PCA_Tri_Edge    = 0x31
} PCATri_Type;

/*--------------------------------------------------------
| @Description: PWM road interrupt flip enum             |
--------------------------------------------------------*/

typedef enum
{
  PWM_Flip_None   = 0x00 , 
  PWM_Flip_First  = 0x05 ,
  PWM_Flip_Second = 0x06 ,
  PWM_Flip_Two    = 0x07 
} PWMFlip_Type;

/*--------------------------------------------------------
| @Description: I2C slave interrupt Trigger enum         |
--------------------------------------------------------*/

typedef enum
{
  I2C_STri_RevStart_Done = 0x40,
  I2C_STri_RevData_Done  = 0x20,
  I2C_STri_SenData_Done  = 0x10,
  I2C_STri_RevStop_Done  = 0x80
} I2CSTri_Type;

/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: Global interrupt switch define           |
--------------------------------------------------------*/

#define    NVIC_GLOBAL_ENABLE()     EA = 1
#define    NVIC_GLOBAL_DISABLE()    EA = 0

/*--------------------------------------------------------
| @Description: EXTI ISR define                          |
--------------------------------------------------------*/

#define	 EXTI0_ISRQ_Handler(void)  EXTI0_ISR(void) interrupt   0  
#define	 EXTI1_ISRQ_Handler(void)  EXTI1_ISR(void) interrupt	 2 
#define	 EXTI2_ISRQ_Handler(void)  EXTI2_ISR(void) interrupt	10 	
#define	 EXTI3_ISRQ_Handler(void)  EXTI3_ISR(void) interrupt	11 	
#define	 EXTI4_ISRQ_Handler(void)  EXTI4_ISR(void) interrupt	16 	

FSCSTATE EXTI0_TRIMode_Init(EXTITri_Type TriState);
FSCSTATE EXTI1_TRIMode_Init(EXTITri_Type TriState);

FSCSTATE NVIC_EXTI0_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_EXTI1_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_EXTI2_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_EXTI3_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_EXTI4_Init(NVICPri_Type Priority,FUNSTATE State);

/*--------------------------------------------------------
| @Description: TIMER ISR define                         |
--------------------------------------------------------*/

#define TIMER0_ISRQ_Handler(void)  TIMER0_ISR(void) interrupt  1
#define TIMER1_ISRQ_Handler(void)  TIMER1_ISR(void) interrupt  3
#define TIMER2_ISRQ_Handler(void)  TIMER2_ISR(void) interrupt 12
#define TIMER3_ISRQ_Handler(void)  TIMER3_ISR(void) interrupt 19
#define TIMER4_ISRQ_Handler(void)  TIMER4_ISR(void) interrupt 20

#define    TIMER2_CLEAR_FLAG()    AUXINTIF &= 0xFE
#define    TIMER3_CLEAR_FLAG()    AUXINTIF &= 0xFD
#define    TIMER4_CLEAR_FLAG()    AUXINTIF &= 0xFB

FSCSTATE NVIC_TIMER0_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_TIMER1_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_TIMER2_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_TIMER3_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_TIMER4_Init(NVICPri_Type Priority,FUNSTATE State);

/*--------------------------------------------------------
| @Description: UART ISR define                          |
--------------------------------------------------------*/

#define UART1_ISRQ_Handler(void)  UART1_ISR(void) interrupt  4
#define UART2_ISRQ_Handler(void)  UART2_ISR(void) interrupt  8
#define UART3_ISRQ_Handler(void)  UART3_ISR(void) interrupt 17
#define UART4_ISRQ_Handler(void)  UART4_ISR(void) interrupt 18

#define    UART1_GET_RX_FLAG()    (SCON  & 0x01)
#define    UART2_GET_RX_FLAG()    (S2CON & 0x01)
#define    UART3_GET_RX_FLAG()    (S3CON & 0x01)
#define    UART4_GET_RX_FLAG()    (S4CON & 0x01)
				   		    
#define    UART1_GET_TX_FLAG()    (SCON  & 0x02)
#define    UART2_GET_TX_FLAG()    (S2CON & 0x02)
#define    UART3_GET_TX_FLAG()    (S3CON & 0x02)
#define    UART4_GET_TX_FLAG()    (S4CON & 0x02)

#define	   UART1_CLEAR_RX_FLAG()  SCON  &= ~0x01
#define	   UART2_CLEAR_RX_FLAG()  S2CON &= ~0x01
#define	   UART3_CLEAR_RX_FLAG()  S3CON &= ~0x01
#define	   UART4_CLEAR_RX_FLAG()  S4CON &= ~0x01
   
#define	   UART1_CLEAR_TX_FLAG()  SCON  &= ~0x02
#define	   UART2_CLEAR_TX_FLAG()  S2CON &= ~0x02
#define	   UART3_CLEAR_TX_FLAG()  S3CON &= ~0x02
#define	   UART4_CLEAR_TX_FLAG()  S4CON &= ~0x02

FSCSTATE NVIC_UART1_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_UART2_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_UART3_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_UART4_Init(NVICPri_Type Priority,FUNSTATE State);

/*--------------------------------------------------------
| @Description: COMP ISR define                          |
--------------------------------------------------------*/

#define COMP_ISRQ_Handler(void)  COMP_ISR(void) interrupt  21

#define COMP_GET_FLAG()        (CMPCR1 &= 0x40)
#define COMP_CLEAR_FLAG()        CMPCR1 &= 0xBF
#define COMP_GET_RES()          (CMPCR1 & 0x01)

FSCSTATE NVIC_COMP_Init(NVICPri_Type Priority,COMPTri_Type State);

/*--------------------------------------------------------
| @Description: ADC ISR define                           |
--------------------------------------------------------*/

#define ADC_ISRQ_Handler(void)  ADC_ISR(void) interrupt  5

#define ADC_GET_FLAG()       (ADC_CONTR & 0x20)
#define ADC_CLEAR_FLAG()      ADC_CONTR &= 0xDF

FSCSTATE NVIC_ADC_Init(NVICPri_Type Priority,FUNSTATE State);

/*--------------------------------------------------------
| @Description: LVD ISR define                           |
--------------------------------------------------------*/

#define LVD_ISRQ_Handler(void)  LVD_ISR(void) interrupt  6

#define LVD_GET_FLAG()       (PCON & 0x20)
#define LVD_CLEAR_FLAG()      PCON &= 0xDF

FSCSTATE NVIC_LVD_Init(NVICPri_Type Priority,FUNSTATE State);

/*--------------------------------------------------------
| @Description: PCA ISR define                           |
--------------------------------------------------------*/

#define PCA_ISRQ_Handler(void)  PCA_ISR(void) interrupt  7

#define PCA_CNT_GET_FLAG()      CF
#define PCA_CNT_CLEAR_FLAG()    CF = 0

#define PCA0_GET_FLAG()           CCF0
#define PCA1_GET_FLAG()           CCF1
#define PCA2_GET_FLAG()           CCF2
#define PCA3_GET_FLAG()           CCF3

#define PCA0_CLEAR_FLAG()     CCF0 = 0
#define PCA1_CLEAR_FLAG()     CCF1 = 0
#define PCA2_CLEAR_FLAG()     CCF2 = 0
#define PCA3_CLEAR_FLAG()     CCF3 = 0

FSCSTATE NVIC_PCA_CNT_Init(NVICPri_Type Priority,FUNSTATE State);

FSCSTATE NVIC_PCA0_TIM_POP_Init(FUNSTATE State);
FSCSTATE NVIC_PCA1_TIM_POP_Init(FUNSTATE State);
FSCSTATE NVIC_PCA2_TIM_POP_Init(FUNSTATE State);
FSCSTATE NVIC_PCA3_TIM_POP_Init(FUNSTATE State);

FSCSTATE NVIC_PCA0_PWM_CAP_Init(PCATri_Type TriState);
FSCSTATE NVIC_PCA1_PWM_CAP_Init(PCATri_Type TriState);
FSCSTATE NVIC_PCA2_PWM_CAP_Init(PCATri_Type TriState);
FSCSTATE NVIC_PCA3_PWM_CAP_Init(PCATri_Type TriState);

/*--------------------------------------------------------
| @Description: PWM ISR define                           |
--------------------------------------------------------*/

#define PWM_ISRQ_Handler(void)          PWM_ISR(void) interrupt 22
#define PWM_ABD_ISRQ_Handler(void)  PWM_ABD_ISR(void) interrupt 23

#define PWM_CNT_RTZ_GET_FLAG()    (PWMCFG &  0x80)
#define PWM_ABD_GET_FLAG()        (PWMFDCR & 0x01)

#define PWM0_GET_FLAG()           (PWMIF  &  0x01)
#define PWM1_GET_FLAG()           (PWMIF  &  0x02)
#define PWM2_GET_FLAG()           (PWMIF  &  0x04)
#define PWM3_GET_FLAG()           (PWMIF  &  0x08)
#define PWM4_GET_FLAG()           (PWMIF  &  0x10)
#define PWM5_GET_FLAG()           (PWMIF  &  0x20)
#define PWM6_GET_FLAG()           (PWMIF  &  0x40)
#define PWM7_GET_FLAG()           (PWMIF  &  0x80)

#define PWM_CNT_RTZ_CLEAR_FLAG()  PWMCFG  &= 0x7F
#define PWM_ABD_CLEAR_FLAG()      PWMFDCR &= 0xFE

#define PWM0_CLEAR_FLAG()         PWMIF &= 0xFE
#define PWM1_CLEAR_FLAG()         PWMIF &= 0xFD
#define PWM2_CLEAR_FLAG()         PWMIF &= 0xFB
#define PWM3_CLEAR_FLAG()         PWMIF &= 0xF7
#define PWM4_CLEAR_FLAG()         PWMIF &= 0xEF
#define PWM5_CLEAR_FLAG()         PWMIF &= 0xDF
#define PWM6_CLEAR_FLAG()         PWMIF &= 0xBF
#define PWM7_CLEAR_FLAG()         PWMIF &= 0x7F

FSCSTATE NVIC_PWM_CNT_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_PWM_ABD_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVIC_PWM0_Init(PWMFlip_Type Flstate);
FSCSTATE NVIC_PWM1_Init(PWMFlip_Type Flstate);
FSCSTATE NVIC_PWM2_Init(PWMFlip_Type Flstate);
FSCSTATE NVIC_PWM3_Init(PWMFlip_Type Flstate);
FSCSTATE NVIC_PWM4_Init(PWMFlip_Type Flstate);
FSCSTATE NVIC_PWM5_Init(PWMFlip_Type Flstate);
FSCSTATE NVIC_PWM6_Init(PWMFlip_Type Flstate);
FSCSTATE NVIC_PWM7_Init(PWMFlip_Type Flstate);

/*--------------------------------------------------------
| @Description: SPI ISR define                           |
--------------------------------------------------------*/

#define SPI_ISRQ_Handler(void)          SPI_ISR(void) interrupt 9

#define SPI_GET_FLAG()                  (SPSTAT & 0x80)
#define SPI_CLEAR_FLAG()                 SPSTAT = 0xC0

FSCSTATE NVIC_SPI_Init(NVICPri_Type Priority,FUNSTATE State);

/*--------------------------------------------------------
| @Description: I2C ISR define                           |
--------------------------------------------------------*/

#define I2C_ISRQ_Handler(void)          I2C_ISR(void) interrupt 24

#define I2C_GET_MASTER_FLAG()           (I2CMSST & 0x40)
#define I2C_GET_REVSTART_FLAG()         (I2CMLST & 0x40)
#define I2C_GET_REVDATA_FLAG()          (I2CMLST & 0x20)
#define I2C_GET_SENDATA_FLAG()          (I2CMLST & 0x10)
#define I2C_GET_REVSTOP_FLAG()          (I2CMLST & 0x08)

#define I2C_CLEAR_MASTER_FLAG()          I2CMSST &= 0xBF
#define I2C_CLEAR_REVSTART_FLAG()        I2CMLST &= 0xBF
#define I2C_CLEAR_REVDATA_FLAG()         I2CMLST &= 0xDF
#define I2C_CLEAR_SENDATA_FLAG()         I2CMLST &= 0xEF
#define I2C_CLEAR_REVSTOP_FLAG()         I2CMLST &= 0xF7

FSCSTATE NVCI_I2C_Master_Init(NVICPri_Type Priority,FUNSTATE State);
FSCSTATE NVCI_I2C_Slave_Init(NVICPri_Type Priority,I2CSTri_Type TriState);


#endif
/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/

